package com.infore.statisticsAndAlarm.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.infore.statisticsAndAlarm.bo.DatasChartsBo;
import com.infore.statisticsAndAlarm.constant.Constants;
import com.infore.statisticsAndAlarm.constant.ResultConstants;
import com.infore.statisticsAndAlarm.dto.WaterInstructionsDataDto;
import com.infore.statisticsAndAlarm.entity.DayData;
import com.infore.statisticsAndAlarm.enums.ResultStatus;
import com.infore.statisticsAndAlarm.externalInterface.IDataReceivingService;
import com.infore.statisticsAndAlarm.externalInterface.IStieMangementService;
import com.infore.statisticsAndAlarm.outsideEntity.Project;
import com.infore.statisticsAndAlarm.repository.IDayDataRepository;
import com.infore.statisticsAndAlarm.repository.IMonthDataRepository;
import com.infore.statisticsAndAlarm.repository.IWeekDataRespository;
import com.infore.statisticsAndAlarm.select.util.PageObject;
import com.infore.statisticsAndAlarm.service.IDataReportsService;
import com.infore.statisticsAndAlarm.util.ArrayUtils;
import com.infore.statisticsAndAlarm.util.CookiesUtil;
import com.infore.statisticsAndAlarm.util.DateUtil;
import com.infore.statisticsAndAlarm.util.MyBeanUtils;
import com.infore.statisticsAndAlarm.util.ObjectMapperUtil;
import com.infore.statisticsAndAlarm.util.Page;
import com.infore.statisticsAndAlarm.util.PageUtil;
import com.infore.statisticsAndAlarm.util.ReflexUtil;
import com.infore.statisticsAndAlarm.util.ResultEntity;
import com.infore.statisticsAndAlarm.util.ScaleUtil;
import com.infore.statisticsAndAlarm.util.WaterLevelUtils;
import com.infore.statisticsAndAlarm.util.WaterQualityUtil;
import com.infore.statisticsAndAlarm.vo.ChartsVo;

@Service
public class DataReprotsServiceImpl implements IDataReportsService {

	@Autowired
	private IDayDataRepository iDayDataRepository;
	@Autowired
	private IStieMangementService iStieMangementService;
	@Autowired
	private ObjectMapperUtil ObjectMapperUtil;
	@Autowired
	private IDataReceivingService iDataReceivingService;
	@Autowired
	private IMonthDataRepository iMonthDataRepository;
	@Autowired
	private WaterLevelUtils WaterLevelUtils;
	@Autowired
	private IWeekDataRespository iWeekDataRespository;
	@Value("${sdcjgyyAllMn}")
	private String sdcjgyyAllMn; //配置文件配置的物理保存地址

	@SuppressWarnings("unchecked")
	public ResultEntity queryDatasStageReports(DatasChartsBo bo, PageObject pageObject) {
		// 查询参数
		List<String> stationCodes = ArrayUtils.stringToList(bo.getStationCode());
		List<String> projectList = ArrayUtils.stringToList(bo.getFactorCode());
		Date startDate = DateUtil.stringToDate(bo.getStartTime(), DateUtil.DATE_SMALL_STR);
		Date endDate = DateUtil.stringToDate(bo.getEndTime(), DateUtil.DATE_SMALL_STR);
		List<DayData> dayDatas = iDayDataRepository
				.findByStationCodeInAndFactorCodeInAndDataTimeBetweenOrderByDataTimeAsc(stationCodes, projectList,
						startDate, endDate);
		// 结果集封装
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();// 结果集
		Map<String, Object> resultMap = new HashMap<String, Object>();// 存放具体值的集合
		Map<String, Object> valueMap = new HashMap<String, Object>();// 记录站点mn日期和因子传输对象
		List<WaterInstructionsDataDto> InstructionsList = new ArrayList<WaterInstructionsDataDto>();
		List<WaterInstructionsDataDto> everyProject = new ArrayList<WaterInstructionsDataDto>();// 每个因子水质等级
		// 获得因子信息
		//ResultEntity projectResultEntity = iStieMangementService.queryProjectByProjectCode();
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByMn(stationCodes);
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		for (int i = 0; i < dayDatas.size(); i++) {
			DayData dayData = dayDatas.get(i);
			String mn = dayData.getStationCode();
			String code = dayData.getFactorCode();
			String avgVlave = dayData.getDataValueAvg();
			String dayTime = DateUtil.dateToString(dayData.getDataTime(), DateUtil.DATE_SMALL_STR);
			String waterLevel = "";
			String decimalDigits = "3";
			if (projectMap != null) {
				Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(code),
						Project.class);
				if (project != null) {
					decimalDigits = project.getDecimalDigits();
				}
			}
			avgVlave = ScaleUtil.sciCalProjectString(avgVlave, decimalDigits);
			// 如果记录不存在，则新增一条记录；否则追加因子值，并更新水质等级
			if (!valueMap.containsKey(mn + dayTime)) {
				resultMap = new HashMap<String, Object>();
				WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();// 计算水质等级封装类
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, code, String.class, avgVlave);
				InstructionsList.add(Instructions);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				waterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put("time", dayTime);// 时间
				resultMap.put("mn", mn);// 站点mn
				resultMap.put(code, avgVlave);// 因子编码和平均值
				resultMap.put("level", waterLevel);// 实际水质等级
				WaterInstructionsDataDto everyLevel = new WaterInstructionsDataDto();// 计算每个因子水质等级
				ReflexUtil.setValue(everyLevel, WaterInstructionsDataDto.class, code, String.class, avgVlave);
				everyProject.add(everyLevel);
				WaterQualityUtil.setWaterQuality(everyProject);
				String everyWaterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(everyLevel.getWaterQuality());
				resultMap.put(code + "level", everyWaterLevel);// 每个因子的水质等级--因子编码+水质等级
				result.add(resultMap);
				valueMap.put(mn + dayTime, Instructions);
			} else {
				WaterInstructionsDataDto Instructions = (WaterInstructionsDataDto) valueMap.get(mn + dayTime);
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, code, String.class, avgVlave);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				waterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put(code, dayData.getDataValueAvg());
				resultMap.put("level", waterLevel);
				WaterInstructionsDataDto everyLevel = new WaterInstructionsDataDto();// 计算每个因子水质等级
				ReflexUtil.setValue(everyLevel, WaterInstructionsDataDto.class, code, String.class, avgVlave);
				everyProject.add(everyLevel);
				WaterQualityUtil.setWaterQuality(everyProject);
				String everyWaterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(everyLevel.getWaterQuality());
				resultMap.put(code + "level", everyWaterLevel);
			}
		}
		// 如果因子值在结果集中不存在，则需要设置为空
		for (String projectCode : projectList) {
			for (Map<String, Object> map : result) {
				if (!map.containsKey(projectCode)) {
					map.put(projectCode, "");
					map.put(projectCode + "level", "");
				}
			}
		}
		// 分页设置
		if (pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null) {
			Page<Map<String, Object>> page = PageUtil.inventedPage(result, pageObject);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
		} else {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
		}
	}

	@SuppressWarnings("unchecked")
	public ResultEntity WaterQualityReports(String startTime, String endTime, String projectCode, String provinceId,
			String basinnId, String stationClassifyId, String stationTypeId, String operCompanyId, String stationName,
			String userId, PageObject pageObject,HttpServletRequest request) {
		// 得到站点信息
    	String platfrom = CookiesUtil.getCookieValue(request, "platformId");
    	String appCode = CookiesUtil.getCookieValue(request, "appCode");
    	userId = CookiesUtil.getCookieValue(request, "userId");
    	String admin = CookiesUtil.getCookieValue(request, "admin");
		ResultEntity resultEntity = iStieMangementService.queryStationMnByInfo(provinceId, basinnId, stationClassifyId,
				stationTypeId, operCompanyId, stationName, userId,platfrom,appCode,admin);
		List<Map<String, Object>> stations = ObjectMapperUtil.convertListMap(resultEntity);
		if (!(stations.size() > 0)) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(stations);
		}
		// 得到天数据信息
		Date startDate = DateUtil.stringToDate(startTime, DateUtil.DATE_SMALL_STR);
		Date endDate = DateUtil.stringToDate(endTime, DateUtil.DATE_SMALL_STR);
		List<String> factorList = ArrayUtils.stringToList(projectCode);
		// 按天查询数据得到每个因子和站点信息
		List<Map<String, Object>> datas = iDayDataRepository.queryInfoGroupBy(startDate, endDate, factorList);
		Map<String, List<Map<String, Object>>> groupDatas = datas.stream()
				.collect(Collectors.groupingBy(g -> g.get("STATION").toString()));// 对结果集进行分组
		// 获得因子信息
		List<String> mns = stations.stream().map(m->m.get("stationMn").toString()).collect(Collectors.toList());
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByMn(mns);
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// 结果集
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		// 匹配站点和数据信息，并进行封装
		for (Map<String, Object> stationInfo : stations) {
			String statName = stationInfo.get("stationName") == null ? "" : stationInfo.get("stationName").toString();// 站点名称
			String statRiver = stationInfo.get("stationRiver") == null ? ""
					: stationInfo.get("stationRiver").toString();// 河流
			String basiName = stationInfo.get("basinnName") == null ? "" : stationInfo.get("basinnName").toString();// 流域
			String areaName = stationInfo.get("areaName") == null ? "" : stationInfo.get("areaName").toString();// 区域
			String statMn = stationInfo.get("stationMn") == null ? "" : stationInfo.get("stationMn").toString();// 站点mn
			Map<String, Object> monitorData = new HashMap<String, Object>();// 监测数据
			List<WaterInstructionsDataDto> synAvgs = new ArrayList<WaterInstructionsDataDto>();// 计算综合平均水质
			WaterInstructionsDataDto synAvg = new WaterInstructionsDataDto();// 综合平均水质等级
			List<WaterInstructionsDataDto> synMaxs = new ArrayList<WaterInstructionsDataDto>();// 计算综合最大水质
			WaterInstructionsDataDto synMax = new WaterInstructionsDataDto();// 综合最大水质等级
			List<WaterInstructionsDataDto> synMins = new ArrayList<WaterInstructionsDataDto>();// 计算综合最小水质
			WaterInstructionsDataDto synMin = new WaterInstructionsDataDto();// 综合最小水质等级
			Map<String, Object> factorLevel = new HashMap<String, Object>();// 记录每个因子水质等级
			Map<String, Object> flagMap = new HashMap<String, Object>();// falg因子存在标志类
			List<Map<String, Object>> groupData = groupDatas.get(statMn);// 得到该站点下所有数据
			if (groupData != null && groupData.size() > 0) {
				for (Map<String, Object> data : groupData) {
					String factorCode = data.get("FACTOR").toString();// 数据统计查询因子编码
					String maxString = data.get("MAX").toString();// 最大值
					String minString = data.get("MIN").toString();// 最小值
					String scopeString = minString + "-" + maxString;// 数据统计查询最大最小值
//					String stationString = data.get("STATION") + "";// 数据统计查询站点编码
					String avgString = data.get("AVG").toString();// 数据统计查询均值
					// String stationCodeString = stationInfo.get("stationMn") + "";
					// 设置值
					flagMap.put(factorCode, "");
					List<WaterInstructionsDataDto> InstructionsAvg = new ArrayList<WaterInstructionsDataDto>();// 计算平均水质等级
					WaterInstructionsDataDto InstructionAvg = new WaterInstructionsDataDto();// 平均水质等级封装类
					List<WaterInstructionsDataDto> InstructionsMax = new ArrayList<WaterInstructionsDataDto>();// 计算最大水质等级
					WaterInstructionsDataDto InstructionMax = new WaterInstructionsDataDto();// 最大水质等级封装类
					List<WaterInstructionsDataDto> InstructionsMin = new ArrayList<WaterInstructionsDataDto>();// 计算最小水质等级
					WaterInstructionsDataDto InstructionMin = new WaterInstructionsDataDto();// 最小水质等级封装类
					String projectUnit = "";
					String decimalDigits = "3";
					if (projectMap != null) {
						Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(factorCode),
								Project.class);
						projectUnit = project.getProjectUnit();
						decimalDigits = project.getDecimalDigits();
					}
					avgString = ScaleUtil.sciCalProjectString(avgString, decimalDigits);// 数据修约
					maxString = ScaleUtil.sciCalProjectString(maxString, decimalDigits);// 最大值
					minString = ScaleUtil.sciCalProjectString(minString, decimalDigits);// 最小值
					scopeString = minString + "-" + maxString;// 数据统计查询最大最小值
					monitorData.put(factorCode + "unit", projectUnit);// 因子编码和因子单位
					monitorData.put("stationName", statName);// 站点名称
					monitorData.put("stationRiver", statRiver);// 站点和刘
					monitorData.put("basinnName", basiName);// 流域名称
					monitorData.put("areaName", areaName);// 区域名称
					monitorData.put("stationMn", statMn);// 站点mn
					monitorData.put(factorCode + "monitorAVG", avgString);// 因子编码和平均值
					monitorData.put(factorCode + "monitorSCOPE", scopeString);// 因子编码和最大最小值
					monitorData.put("AudioContent", "监测数据");// 监测数据
					monitorData.put("synLevel" + "AVG", "");// 综合平均值
					monitorData.put("synLevel" + "SCOPE", "");// 综合最大最小值
					// 计算平均水质
					ReflexUtil.setValue(InstructionAvg, WaterInstructionsDataDto.class, factorCode, String.class,
							avgString);
					ReflexUtil.setValue(synAvg, WaterInstructionsDataDto.class, factorCode, String.class, avgString);
					InstructionsAvg.add(InstructionAvg);
					synAvgs.add(synAvg);
					// 计算范围内最大水质
					ReflexUtil.setValue(InstructionMax, WaterInstructionsDataDto.class, factorCode, String.class,
							maxString);
					InstructionsMax.add(InstructionMax);
					ReflexUtil.setValue(synMax, WaterInstructionsDataDto.class, factorCode, String.class, maxString);
					synMaxs.add(synMax);
					// 计算范围内最小水质
					ReflexUtil.setValue(InstructionMin, WaterInstructionsDataDto.class, factorCode, String.class,
							minString);
					InstructionsMin.add(InstructionMin);
					ReflexUtil.setValue(synMin, WaterInstructionsDataDto.class, factorCode, String.class, minString);
					synMins.add(synMin);
					WaterQualityUtil.setWaterQuality(InstructionsAvg);// 计算水质级别
					WaterQualityUtil.setWaterQuality(InstructionsMax);// 计算水质级别
					WaterQualityUtil.setWaterQuality(InstructionsMin);// 计算水质级别
					String waterLevelMin = WaterLevelUtils
							.exchangeWaterLevelToChinese(InstructionMin.getWaterQuality());
					String waterLevelMax = WaterLevelUtils
							.exchangeWaterLevelToChinese(InstructionMax.getWaterQuality());
					factorLevel.put(factorCode + "monitorSCOPE", waterLevelMin + "-" + waterLevelMax);

					String waterLevelAvg = WaterLevelUtils
							.exchangeWaterLevelToChinese(InstructionAvg.getWaterQuality());
					factorLevel.put(factorCode + "monitorAVG", waterLevelAvg);
				}
			}
			// 如果因子在结果集中不存在，需要设置为空
			factorList.remove("");
			factorList.remove(null);
			try {
				for (String code : factorList) {
					if (!flagMap.containsKey(code)) {
						// String projectName = "";
						String projectUnit = "";
						if (projectMap != null) {
							Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(code),
									Project.class);
							// projectName = project.getProjectName();
							projectUnit = project.getProjectUnit();
						}
						monitorData.put(code + "unit", projectUnit);
						monitorData.put("stationName", statName);
						monitorData.put("stationRiver", statRiver);
						monitorData.put("basinnName", basiName);
						monitorData.put("areaName", areaName);
						monitorData.put("stationMn", statMn);
						monitorData.put(code + "monitorAVG", "");
						monitorData.put(code + "monitorSCOPE", "");
						monitorData.put("AudioContent", "监测数据");
						monitorData.put("synLevel" + "AVG", "");
						monitorData.put("synLevel" + "SCOPE", "");
						factorLevel.put(code + "unit", projectUnit);
						factorLevel.put("stationName", statName);
						factorLevel.put("stationRiver", statRiver);
						factorLevel.put("basinnName", basiName);
						factorLevel.put("areaName", areaName);
						factorLevel.put("stationMn", statMn);
						factorLevel.put(code + "AVGLevel", "");
						factorLevel.put(code + "SCOPELevel", "");
						factorLevel.put("synLevel" + "SCOPE", "" + "-" + "");
						factorLevel.put("AudioContent", "水质类别");
					}
				}
			} catch (Exception e) {
				//e.printStackTrace();
			}
			WaterQualityUtil.setWaterQuality(synAvgs);// 计算水质级别
			WaterQualityUtil.setWaterQuality(synMaxs);// 计算水质级别
			WaterQualityUtil.setWaterQuality(synMins);// 计算水质级别
			String synLevelAvg = WaterLevelUtils.exchangeWaterLevelToChinese(synAvg.getWaterQuality());
			String synLevelMin = WaterLevelUtils.exchangeWaterLevelToChinese(synMin.getWaterQuality());
			String synLevelMax = WaterLevelUtils.exchangeWaterLevelToChinese(synMax.getWaterQuality());
			factorLevel.put("stationName", statName);
			factorLevel.put("stationRiver", statRiver);
			factorLevel.put("basinnName", basiName);
			factorLevel.put("areaName", areaName);
			factorLevel.put("stationMn", statMn);
			factorLevel.put("synLevel" + "AVG", synLevelAvg);
			factorLevel.put("synLevel" + "SCOPE", synLevelMin + "-" + synLevelMax);
			factorLevel.put("AudioContent", "水质类别");
			result.add(monitorData);
			result.add(factorLevel);
		}
		// 分页设置
		if (pageObject != null && pageObject.getPageNumber() != null && pageObject.getPageSize() != null) {
			Page<Map<String, Object>> page = PageUtil.inventedPage(result, pageObject);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setPage(page);
		} else {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(result);
		}
	}

	@SuppressWarnings("unchecked")
	public ResultEntity audioSingleReports(String timeType, String time, String stationMn, String weekSatrt,
			String weekEnd) {
		// 参数判断
		if (StringUtils.isBlank(time) && StringUtils.isBlank(weekSatrt) && StringUtils.isBlank(weekSatrt)) {
			return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.EMPTY_PARAMETER);
		}
		List<String> mnList = ArrayUtils.stringToList(stationMn);
		// 查询单站点下因子code集合
		ResultEntity projectCodesEntity = iStieMangementService.queryProjectByStationMn(stationMn);
		List<String> projectCodes = ObjectMapperUtil.convertList(projectCodesEntity, String.class);// 站点下因子集合
		String startTime = "";
		String endTime = "";
		String pattern = "";
		List<Map<String, Object>> dataResult = new ArrayList<Map<String, Object>>();// 小时天月数据结果集
		// 获得因子信息
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByProjectCode();
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// projectMap.remove(projectCodes);
		List<Map<String, Object>> projectTotaList = new ArrayList<Map<String, Object>>();
		if (projectMap != null) {
			for (Entry<String, Project> projectCode : projectMap.entrySet()) {
				if (projectCodes.contains(projectCode.getKey())) {
					Project project = MyBeanUtils.convertMapToBean(
							(Map<String, Object>) projectMap.get(projectCode.getKey()), Project.class);
					Map<String, Object> projectMapList = new HashMap<String, Object>();
					projectMapList.put("unit", project.getProjectUnit());
					projectMapList.put("code", project.getProjectCode());
					projectMapList.put("name", project.getProjectName());
					projectTotaList.add(projectMapList);
				}
			}
		}
		// 判断站点编码和因子因子编码是否存在
		if (StringUtils.isBlank(stationMn) || projectCodes == null || projectCodes.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(null);
		}
		// 按时间分发给不同的接口查询，并设置起止时间
		if ("day".equals(timeType)) {
			startTime = time + " 00:00:00";
			endTime = time + " 23:59:59";
			pattern = DateUtil.DATE_SMALL_YMDHH;// 时间转换格式，天要查小时表
			String codeString = StringUtils.join(projectCodes, ",");
			ResultEntity resultEntity = iDataReceivingService.audioSingReport(stationMn, codeString, startTime,
					endTime);
			if (resultEntity.getCode() != 0) {
				return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_PROJECT);
			}
			dataResult = ObjectMapperUtil.convertListMap(resultEntity);
		} else if ("month".equals(timeType)) {
			startTime = time + "-01";
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			Date endDate1 = DateUtil.stringToDate(time, DateUtil.DATE_SMALL_YM);
			endTime = DateUtil.getLastDayM(endDate1, pattern);// 得到当月最后一天
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iDayDataRepository.audioSingReport(startDate, endDate, projectCodes, mnList);
		} else if ("week".equals(timeType)) {
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			dataResult = iWeekDataRespository.audioSingReport(weekSatrt, weekEnd, projectCodes, mnList);
		} else {
			pattern = DateUtil.DATE_SMALL_YM;// 时间转换格式
			startTime = time + "-01";
			endTime = time + "-12";
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iMonthDataRepository.audioSingReport(startDate, endDate, projectCodes, mnList);
		}
		return audioSingleReportsAll(projectTotaList, projectCodes, dataResult, startTime, endTime, pattern, timeType, stationMn);
	}

	@SuppressWarnings("unchecked")
	public ResultEntity audioSingleReportsAll(List<Map<String, Object>> projectTotaList, List<String> projectCodes,
			List<Map<String, Object>> dataResult, String startTime, String endTime, String pattern, String timeType
			,String stationMn) {
		// 查询因子值数据集
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();// 结果集
		if (dataResult == null || dataResult.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
					.setInfo(projectTotaList);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();// 存储每一条数据
		Map<String, Object> valueMap = new HashMap<String, Object>();// 水质等级计算，存储记录类
		List<WaterInstructionsDataDto> InstructionsList = new ArrayList<WaterInstructionsDataDto>();
		Map<String, Object> indexMapAvg = new HashMap<String, Object>();// 因子均值，存储记录类
		Map<String, Object> indexMapMax = new HashMap<String, Object>();// 因子最大值，存储记录类
		Map<String, Object> indexMapMin = new HashMap<String, Object>();// 因子最小值，存储记录类
		// 获得因子信息
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByProjectCode();
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// 遍历因子值数据集，设置相关的值
		for (Map<String, Object> dataResultMap : dataResult) {
			String time;
			if ("day".equals(timeType)) {
				// 远程调用小时数据接口时，服务间调用会将时间类型转化成字符串
				// 截取格林尼治时间为标准时间
				String indexStrings = dataResultMap.get("TIME").toString();
				time = indexStrings.substring(0, 10) + " " + indexStrings.substring(11, 19);
			} else if ("week".equals(timeType)) {
				time = DateUtil.dateToString((Date) (dataResultMap.get("TIME")), pattern);
				time = DateUtil.getMonthWeekString(time);
			} else {
				time = DateUtil.dateToString((Date) (dataResultMap.get("TIME")), pattern);
			}
			String mn = dataResultMap.get("MN").toString();
			String projectCode = dataResultMap.get("CODE").toString();
			String value = dataResultMap.get("VALUE") == null ? "" : dataResultMap.get("VALUE").toString();
			String decimalDigits = "3";
			if (projectMap != null) {// 拿到因子信息
				Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(projectCode),
						Project.class);
				decimalDigits = project.getDecimalDigits();
			}
			value = ScaleUtil.sciCalProjectString(value, decimalDigits);// 数据修约
			// 如果当前站点当前时间段下没有因子相关信息，则创建新的，否则在原有的上追加
			if (!valueMap.containsKey(mn + time)) {
				resultMap = new HashMap<String, Object>();
				WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();// 计算水质等级封装类
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				InstructionsList.add(Instructions);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put("time", time);// 时间
				resultMap.put(projectCode, value);// 因子编号和值
				resultMap.put("level", waterLevel);// 实际水质等级
				list.add(resultMap);
				valueMap.put(mn + time, Instructions);
			} else {
				WaterInstructionsDataDto Instructions = (WaterInstructionsDataDto) valueMap.get(mn + time);
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put(projectCode, value);
				resultMap.put("level", waterLevel);
			}
		}
		// 设置最大值最小值平均值
		for (String codeString : projectCodes) {
			// 如果不存在该因子则补空，存在则进行计算
			List<Double> codeStringList = list.stream().filter(t -> t.containsKey(codeString))
					.map(l -> Double.parseDouble(l.get(codeString).toString())).collect(Collectors.toList());
			if (codeStringList.size() != 0) {
				String decimalDigits = "3";
				if (projectMap != null) {
					Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(codeString),
							Project.class);
					decimalDigits = project.getDecimalDigits();
				}
				double max = Collections.max(codeStringList);
				double min = Collections.min(codeStringList);
				double avg = codeStringList.stream().collect(Collectors.averagingDouble(Double::doubleValue));
				indexMapMax.put(codeString, ScaleUtil.sciCalProjectString(max + "", decimalDigits));// 最大值
				indexMapMin.put(codeString, ScaleUtil.sciCalProjectString(min + "", decimalDigits));// 最小值
				indexMapAvg.put(codeString, ScaleUtil.sciCalProjectString(avg + "", decimalDigits));// 平均值
			}
//			for (Map<String, Object> map : list) {
//				System.out.println(map.keySet());
//				System.out.println(codeString);
//				if(!map.containsKey(codeString)) {
//					map.put(codeString, "");
//					indexMapMax.put(codeString, "");
//					indexMapMin.put(codeString, "");
//					indexMapAvg.put(codeString, "");
//				}
//			}
		}
		// 设置最大值最小值均值
		indexMapMax.put("time", "最大值");
		indexMapMax.put("level", "");
		indexMapMin.put("time", "最小值");
		indexMapMin.put("level", "");
		indexMapAvg.put("time", "平均值");
		indexMapAvg.put("level", "");
		list.add(indexMapMax);
		list.add(indexMapMin);
		list.add(indexMapAvg);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
				.setInfo(projectTotaList);
	}

	@SuppressWarnings("unchecked")
	public ResultEntity audioMoreReports(String timeType, String time, String stationMn, String weekSatrt,
			String weekEnd) {
		if (StringUtils.isEmpty(stationMn)) {
			stationMn = sdcjgyyAllMn;
		}
		// 参数判断
		if (StringUtils.isBlank(time) && StringUtils.isBlank(weekSatrt) && StringUtils.isBlank(weekSatrt)) {
			return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.EMPTY_PARAMETER);
		}
		List<String> mnList = ArrayUtils.stringToList(stationMn);
		// 得到所有因子编码
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByMn(mnList);
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		List<String> projectCodes = new ArrayList<>(projectMap.keySet());
		// 获得因子信息
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		List<Map<String, Object>> projectTotaList = new ArrayList<Map<String, Object>>();
		if (projectMap != null) {
			for (Entry<String, Project> projectCode : projectMap.entrySet()) {
				Project project = MyBeanUtils
						.convertMapToBean((Map<String, Object>) projectMap.get(projectCode.getKey()), Project.class);
				Map<String, Object> projectMapList = new HashMap<String, Object>();
				projectMapList.put("unit", project.getProjectUnit());// 因子单位
				projectMapList.put("code", project.getProjectCode());// 因子编码
				projectMapList.put("name", project.getProjectName());// 因子名称
				projectTotaList.add(projectMapList);
			}
		}
		// 站点名称
		ResultEntity stationNameResultEntity = iStieMangementService.queryNameByStationMn(stationMn);
		List<Map<String, Object>> stationNameList = ObjectMapperUtil.convertListMap(stationNameResultEntity);
		Map<String, Object> stationNameMap = new HashMap<String, Object>();
		for (Map<String, Object> map : stationNameList) {
			stationNameMap.put(map.get("MN").toString(), map.get("NAME").toString());
		}
		String startTime = "";
		String endTime = "";
		String pattern = "";
		List<Map<String, Object>> dataResult = new ArrayList<Map<String, Object>>();// 小时天月数据结果集
		// 判断站点编码和因子因子编码是否存在
		if (StringUtils.isBlank(stationMn) || projectCodes == null || projectCodes.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(null);
		}
		// 按时间分发给不同的接口查询，并设置起止时间
		if ("day".equals(timeType)) {
			startTime = time + " 00:00:00";
			endTime = time + " 23:59:59";
			pattern = DateUtil.DATE_SMALL_YMDHH;// 时间转换格式，天要查小时表
			String codeString = StringUtils.join(projectCodes, ",");
			ResultEntity resultEntity = iDataReceivingService.audioMoreReport(stationMn, codeString, startTime,
					endTime);
			if (resultEntity.getCode() != 0) {
				return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_PROJECT);
			}
			dataResult = ObjectMapperUtil.convertListMap(resultEntity);
		} else if ("month".equals(timeType)) {
			startTime = time + "-01";
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			Date endDate1 = DateUtil.stringToDate(time, DateUtil.DATE_SMALL_YM);
			endTime = DateUtil.getLastDayM(endDate1, pattern);// 得到当月最后一天
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iDayDataRepository.audioMoreReport(startDate, endDate, projectCodes, mnList);
		} else if ("week".equals(timeType)) {
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			dataResult = iWeekDataRespository.audioMoreReport(weekSatrt, weekEnd, projectCodes, mnList);
		} else {
			pattern = DateUtil.DATE_SMALL_YM;// 时间转换格式
			startTime = time + "-01";
			endTime = time + "-12";
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iMonthDataRepository.audioMoreReport(startDate, endDate, projectCodes, mnList);
		}
		return audioMoreReportsAll(projectResultEntity, projectTotaList, stationNameMap, projectCodes, dataResult,
				startTime, endTime, mnList, pattern);
	}

	@SuppressWarnings({ "unchecked" })
	public ResultEntity audioMoreReportsAll(ResultEntity projectResultEntity, List<Map<String, Object>> projectTotaList,
			Map<String, Object> stationNameMap, List<String> projectCodes, List<Map<String, Object>> dataResult,
			String startTime, String endTime, List<String> mnList, String pattern) {
		// 查询因子值数据集
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();// 结果集
		if (dataResult == null || dataResult.size() == 0) {
			Map<String, Object> messageMap = new HashMap<String, Object>();
			messageMap.put("message", "");
			messageMap.put("project", projectTotaList);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
					.setInfo(messageMap);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();// 存储每一条数据
		Map<String, Object> valueMap = new HashMap<String, Object>();// 水质等级计算，存储记录类
		List<WaterInstructionsDataDto> InstructionsList = new ArrayList<WaterInstructionsDataDto>();
		List<String> keyPollution = new ArrayList<String>();// 主要污染物
		WaterInstructionsDataDto keyPollutionInstruc = new WaterInstructionsDataDto();// 计算当前污染物
		List<WaterInstructionsDataDto> keyPollutionList = new ArrayList<WaterInstructionsDataDto>();
		// 获得因子信息
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// 遍历因子值数据集，设置相关的值
		for (Map<String, Object> dataResultMap : dataResult) {
			String mn = dataResultMap.get("MN").toString();
			String projectCode = dataResultMap.get("CODE").toString();
			String value = dataResultMap.get("VALUE").toString();
			String projectName = "";
			String decimalDigits = "3";
			if (projectMap != null) {
				Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(projectCode),
						Project.class);
				projectName = project.getProjectName();
				decimalDigits = project.getDecimalDigits();
			}
			value = ScaleUtil.sciCalProjectString(value, decimalDigits);// 数据修约
			// 如果当前站点下没有因子相关信息，则创建新的，否则在原有的上追加
			if (!valueMap.containsKey(mn)) {
				resultMap = new HashMap<String, Object>();
				keyPollution = new ArrayList<String>();
				WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();// 计算水质等级封装类
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				InstructionsList.add(Instructions);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterQuery = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put("mn", mn);// 站点mn
				resultMap.put("name", stationNameMap.get(mn));// 站点名称
				resultMap.put(projectCode, value);// 因子编号和因子值
				resultMap.put("level", waterQuery);// 实际水质等级
				resultMap.put("waterCondition", WaterLevelUtils.calculateProject(waterQuery));// 水质状况（优良轻度污染等）
				resultMap.put("compositeIndex", WaterQualityUtil.countCompositeIndex(Instructions));// 综合水质
				// 计算当前污染物是否超过三级
				if ((WaterQualityUtil.getWaterLevel(projectCode, value) > 3) && !keyPollution.contains(projectName)) {
					keyPollution.add(projectName);
				} // 计算设置主要污染物
				resultMap.put("keyPollution", StringUtils.join(keyPollution, ","));
				list.add(resultMap);
				valueMap.put(mn, Instructions);
			} else {
				WaterInstructionsDataDto Instructions = (WaterInstructionsDataDto) valueMap.get(mn);
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterQuery = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put(projectCode, value);
				resultMap.put("level", waterQuery);
				resultMap.put("waterCondition", WaterLevelUtils.calculateProject(waterQuery));
				resultMap.put("compositeIndex", WaterQualityUtil.countCompositeIndex(Instructions));
				// 计算当前污染物是否超过三级
				keyPollutionInstruc = new WaterInstructionsDataDto();
				keyPollutionList = new ArrayList<WaterInstructionsDataDto>();
				ReflexUtil.setValue(keyPollutionInstruc, WaterInstructionsDataDto.class, projectCode, String.class,
						value);
				keyPollutionList.add(keyPollutionInstruc);
				WaterQualityUtil.setWaterQuality(keyPollutionList);
				if ((keyPollutionInstruc.getWaterQuality() != null)
						&& (Integer.parseInt(keyPollutionInstruc.getWaterQuality()) > 3)
						&& !keyPollution.contains(projectName)) {
					keyPollution.add(projectName);
				}
				// 计算设置主要污染物
				resultMap.put("keyPollution", StringUtils.join(keyPollution, ","));
			}
		}
		// 添加因子值数据集中不存在的站点
		List<String> mnTotalStringList = list.stream().map(l -> l.get("mn").toString()).collect(Collectors.toList());// 结果集中的站点mn集合
		String mnTotalString = StringUtils.join(mnTotalStringList, "");// 转化成字符串
		for (String mn : mnList) {
			if (!mnTotalString.contains(mn)) {
				Map<String, Object> mnMap = new HashMap<String, Object>();
				mnMap.put("mn", mn);
				mnMap.put("name", stationNameMap.get(mn));
				mnMap.put("keyPollution", "");
				mnMap.put("waterCondition", "");
				mnMap.put("compositeIndex", "");
				mnMap.put("level", "");
				list.add(mnMap);
				mnTotalString = mnTotalString + mn;
			}
		}
		// 因子值数据集中不存在的因子，需要全部设置为空
		for (String codeString : projectCodes) {
			for (Map<String, Object> map : list) {
				if (!map.containsKey(codeString)) {
					map.put(codeString, "");
				}
			}
		}
		StringBuffer message = new StringBuffer();// 返回的总结提示语
		message.append("所选时段内,所选" + stationNameMap.size() + "个点位中");// 按水质等级对结果集分组
		Map<String, List<Map<String, Object>>> groupList = list.stream()
				.collect(Collectors.groupingBy(x -> x.get("level").toString()));
		groupList.remove("");
		groupList.forEach((k, klist) -> {
			// 得到当前分组下，站点名称集合
			List<String> nameList = klist.stream().map(mapper -> mapper.get("name").toString())
					.collect(Collectors.toList());
			String news1 = "有" + klist.size() + "个站点" + "(" + StringUtils.join(nameList, ",") + ")" + "断面水质达到" + k
					+ "标准" + "评价为" + klist.get(0).get("waterCondition") + ";";
			message.append(news1);
		});
		if ((stationNameMap.size() - mnTotalStringList.size()) > 0) {
			message.append("有" + (stationNameMap.size() - mnTotalStringList.size()) + "个站点没有数据");
		}
		Map<String, Object> messageMap = new HashMap<String, Object>();
		messageMap.put("message", message.toString());
		messageMap.put("project", projectTotaList);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list).setInfo(messageMap);
	}

	public ResultEntity publicWaterQualityReports(String timeType, String time, String queryCode, String queryType,
			String userId,HttpServletRequest request) {
		if (!"station".equals(queryType)) {
			String platfrom = CookiesUtil.getCookieValue(request, "platformId");
			String appCode = CookiesUtil.getCookieValue(request, "appCode");
			userId = CookiesUtil.getCookieValue(request, "userId");
			String admin = CookiesUtil.getCookieValue(request, "admin");
			ResultEntity resultEntity = iStieMangementService.queryStationInfoByCodes(
					queryCode,queryType,userId,platfrom,appCode,admin);
			if (resultEntity.getCode() == 0) {
				List<Map<String, Object>> stationInfo = ObjectMapperUtil.convertListMap(resultEntity);
				List<String> nmList = stationInfo.stream().map(l -> l.get("MN").toString())
						.collect(Collectors.toList());
				if (nmList != null && nmList.size() > 0) {
					return audioMoreReports(timeType, time, StringUtils.join(nmList, ","), "", "");
				} else {
					return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS);
				}
			} else {
				return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS);
			}
		} else {
			return audioMoreReports(timeType, time, queryCode, "", "");
		}
	}

	@SuppressWarnings("unchecked")
	public ResultEntity audioSingleReportsCharts(String timeType, String time, String stationMn, String weekSatrt,
			String weekEnd) {
		// 参数判断
		if (StringUtils.isBlank(time) && StringUtils.isBlank(weekSatrt) && StringUtils.isBlank(weekSatrt)) {
			return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.EMPTY_PARAMETER);
		}
		List<String> mnList = ArrayUtils.stringToList(stationMn);
		// 查询单站点下因子code集合
		ResultEntity projectCodesEntity = iStieMangementService.queryProjectByStationMn(stationMn);
		List<String> projectCodes = ObjectMapperUtil.convertList(projectCodesEntity, String.class);// 站点下因子集合
		String startTime = "";
		String endTime = "";
		String pattern = "";
		List<Map<String, Object>> dataResult = new ArrayList<Map<String, Object>>();// 小时天月数据结果集
		// 获得因子信息
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByProjectCode();
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// projectMap.remove(projectCodes);
		List<Map<String, Object>> projectTotaList = new ArrayList<Map<String, Object>>();
		if (projectMap != null) {
			for (Entry<String, Project> projectCode : projectMap.entrySet()) {
				if (projectCodes.contains(projectCode.getKey())) {
					Project project = MyBeanUtils.convertMapToBean(
							(Map<String, Object>) projectMap.get(projectCode.getKey()), Project.class);
					Map<String, Object> projectMapList = new HashMap<String, Object>();
					projectMapList.put("unit", project.getProjectUnit());
					projectMapList.put("code", project.getProjectCode());
					projectMapList.put("name", project.getProjectName());
					projectTotaList.add(projectMapList);
				}
			}
		}
		// 判断站点编码和因子因子编码是否存在
		if (StringUtils.isBlank(stationMn) || projectCodes == null || projectCodes.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(null);
		}
		// 按时间分发给不同的接口查询，并设置起止时间
		if ("day".equals(timeType)) {
			startTime = time + " 00:00:00";
			endTime = time + " 23:59:59";
			pattern = DateUtil.DATE_SMALL_YMDHH;// 时间转换格式，天要查小时表
			String codeString = StringUtils.join(projectCodes, ",");
			ResultEntity resultEntity = iDataReceivingService.audioSingReport(stationMn, codeString, startTime,
					endTime);
			if (resultEntity.getCode() != 0) {
				return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_PROJECT);
			}
			dataResult = ObjectMapperUtil.convertListMap(resultEntity);
		} else if ("month".equals(timeType)) {
			startTime = time + "-01";
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			Date endDate1 = DateUtil.stringToDate(time, DateUtil.DATE_SMALL_YM);
			endTime = DateUtil.getLastDayM(endDate1, pattern);// 得到当月最后一天
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iDayDataRepository.audioSingReport(startDate, endDate, projectCodes, mnList);
		} else if ("week".equals(timeType)) {
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			dataResult = iWeekDataRespository.audioSingReport(weekSatrt, weekEnd, projectCodes, mnList);
		} else {
			pattern = DateUtil.DATE_SMALL_YM;// 时间转换格式
			startTime = time + "-01";
			endTime = time + "-12";
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iMonthDataRepository.audioSingReport(startDate, endDate, projectCodes, mnList);
		}
		return audioSingleReportsAll_echarts(projectTotaList, projectCodes, dataResult, startTime, endTime, pattern,
				timeType, stationMn);
	}

	@SuppressWarnings("unchecked")
	public ResultEntity audioSingleReportsAll_echarts(List<Map<String, Object>> projectTotaList,
			List<String> projectCodes, List<Map<String, Object>> dataResult, String startTime, String endTime,
			String pattern, String timeType,String stationMn) {
		// 查询因子值数据集
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();// 结果集
		if (dataResult == null || dataResult.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
					.setInfo(projectTotaList);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();// 存储每一条数据
		Map<String, Object> valueMap = new HashMap<String, Object>();// 水质等级计算，存储记录类
		List<WaterInstructionsDataDto> InstructionsList = new ArrayList<WaterInstructionsDataDto>();
		// 初始化echarts
		ChartsVo chartsVo = new ChartsVo();
		List<String> xAxis = new ArrayList<String>(); // X轴数据集
		// List<String> legend = new ArrayList<String>(); // 图例数据集
		List<Map<String, Object>> series = new ArrayList<Map<String, Object>>(); // 系列列表数据集
		Map<String, Object> seriesMap = new HashMap<String, Object>();
		// 获得因子信息
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByProjectCode();
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// 遍历因子值数据集，设置相关的值
		for (Map<String, Object> dataResultMap : dataResult) {
			String time;
			if ("day".equals(timeType)) {
				// 远程调用小时数据接口时，服务间调用会将时间类型转化成字符串
				// 截取格林尼治时间为标准时间
				String indexStrings = dataResultMap.get("TIME").toString();
				time = indexStrings.substring(0, 10) + " " + indexStrings.substring(11, 19);
			} else if ("week".equals(timeType)) {
				time = DateUtil.dateToString((Date) (dataResultMap.get("TIME")), pattern);
				time = DateUtil.getMonthWeekString(time);
			} else {
				time = DateUtil.dateToString((Date) (dataResultMap.get("TIME")), pattern);
			}
			String mn = dataResultMap.get("MN").toString();
			String projectCode = dataResultMap.get("CODE").toString();
			String value = dataResultMap.get("VALUE") == null ? "" : dataResultMap.get("VALUE").toString();
			String decimalDigits = "3";
			if (projectMap != null) {// 拿到因子信息
				Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(projectCode),
						Project.class);
				decimalDigits = project.getDecimalDigits();
			}
			value = ScaleUtil.sciCalProjectString(value, decimalDigits);// 数据修约
			// 如果当前站点当前时间段下没有因子相关信息，则创建新的，否则在原有的上追加
			if (!valueMap.containsKey(mn + time)) {
				resultMap = new HashMap<String, Object>();
				WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();// 计算水质等级封装类
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				InstructionsList.add(Instructions);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put("time", time);// 时间
				resultMap.put(projectCode, value);// 因子编号和值
				resultMap.put("level", waterLevel);// 实际水质等级
				list.add(resultMap);
				valueMap.put(mn + time, Instructions);
			} else {
				WaterInstructionsDataDto Instructions = (WaterInstructionsDataDto) valueMap.get(mn + time);
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterLevel = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put(projectCode, value);
				resultMap.put("level", waterLevel);
			}
		}
		// 封装echarts图表
		// 通过时间分组
		List<String> keyList = new ArrayList<String>(list.get(0).keySet());
		TreeMap<String, List<Map<String, Object>>> glist = list.stream()
				.collect(Collectors.groupingBy(g -> g.get("time").toString(), TreeMap::new, Collectors.toList()));
		glist.forEach((k, klist) -> {
			xAxis.add(k);
			keyList.forEach(key -> {
				List<Object> data = klist.stream()
						.map(m -> m.get(key)).collect(Collectors.toList());
				if (seriesMap.containsKey(key)) {
					List<Object> dataSingle = (List<Object>) seriesMap.get(key);
					dataSingle.addAll(data);
				} else {
					seriesMap.put(key, data);
				}
			});
		});// projectTotaList
			// 获得因子信息
		for (Entry<String, Object> string : seriesMap.entrySet()) {
			Map<String, Object> map = new HashMap<String, Object>();
			String unit = "";
			String name = "";
			if (string.getKey().equals("time") || string.getKey().equals("level")) {
				continue;
			}
			if (!string.getKey().equals("level")) {
				if (projectMap != null) {// 拿到因子信息
					Project project = MyBeanUtils
							.convertMapToBean((Map<String, Object>) projectMap.get(string.getKey()), Project.class);
					unit = project.getProjectUnit();
					name = project.getProjectName();
				}
			}
			map.put("code", string.getKey());
			map.put("name", name);
			map.put("unit", unit);
			map.put("data", string.getValue());
			series.add(map);
		}
		chartsVo.setxAxis(xAxis);
		chartsVo.setSeries(series);
		if (xAxis == null || xAxis.size() == 0 || series == null || series.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(chartsVo);
	}

	@SuppressWarnings("unchecked")
	public ResultEntity audioMoreReportsCharts(String timeType, String time, String stationMn, String weekSatrt,
			String weekEnd) {
		// 参数判断
		if (StringUtils.isBlank(time) && StringUtils.isBlank(weekSatrt) && StringUtils.isBlank(weekSatrt)) {
			return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.EMPTY_PARAMETER);
		}
		List<String> mnList = ArrayUtils.stringToList(stationMn);
		// 得到所有因子编码
		ResultEntity projectResultEntity = iStieMangementService.queryProjectByMn(mnList);
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		List<String> projectCodes = new ArrayList<>(projectMap.keySet());
		// 获得因子信息
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		List<Map<String, Object>> projectTotaList = new ArrayList<Map<String, Object>>();
		if (projectMap != null) {
			for (Entry<String, Project> projectCode : projectMap.entrySet()) {
				Project project = MyBeanUtils
						.convertMapToBean((Map<String, Object>) projectMap.get(projectCode.getKey()), Project.class);
				Map<String, Object> projectMapList = new HashMap<String, Object>();
				projectMapList.put("unit", project.getProjectUnit());// 因子单位
				projectMapList.put("code", project.getProjectCode());// 因子编码
				projectMapList.put("name", project.getProjectName());// 因子名称
				projectTotaList.add(projectMapList);
			}
		}
		// 站点名称
		ResultEntity stationNameResultEntity = iStieMangementService.queryNameByStationMn(stationMn);
		List<Map<String, Object>> stationNameList = ObjectMapperUtil.convertListMap(stationNameResultEntity);
		Map<String, Object> stationNameMap = new HashMap<String, Object>();
		for (Map<String, Object> map : stationNameList) {
			stationNameMap.put(map.get("MN").toString(), map.get("NAME").toString());
		}
		String startTime = "";
		String endTime = "";
		String pattern = "";
		List<Map<String, Object>> dataResult = new ArrayList<Map<String, Object>>();// 小时天月数据结果集
		// 判断站点编码和因子因子编码是否存在
		if (StringUtils.isBlank(stationMn) || projectCodes == null || projectCodes.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(null);
		}
		// 按时间分发给不同的接口查询，并设置起止时间
		if ("day".equals(timeType)) {
			startTime = time + " 00:00:00";
			endTime = time + " 23:59:59";
			pattern = DateUtil.DATE_SMALL_YMDHH;// 时间转换格式，天要查小时表
			String codeString = StringUtils.join(projectCodes, ",");
			ResultEntity resultEntity = iDataReceivingService.audioMoreReport(stationMn, codeString, startTime,
					endTime);
			if (resultEntity.getCode() != 0) {
				return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_PROJECT);
			}
			dataResult = ObjectMapperUtil.convertListMap(resultEntity);
		} else if ("month".equals(timeType)) {
			startTime = time + "-01";
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			Date endDate1 = DateUtil.stringToDate(time, DateUtil.DATE_SMALL_YM);
			endTime = DateUtil.getLastDayM(endDate1, pattern);// 得到当月最后一天
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iDayDataRepository.audioMoreReport(startDate, endDate, projectCodes, mnList);
		} else if ("week".equals(timeType)) {
			pattern = DateUtil.DATE_SMALL_STR;// 时间转换格式，月要查当月下的天表
			dataResult = iWeekDataRespository.audioSingReport(weekSatrt, weekEnd, projectCodes, mnList);
		} else {
			pattern = DateUtil.DATE_SMALL_YM;// 时间转换格式
			startTime = time + "-01";
			endTime = time + "-12";
			Date startDate = DateUtil.stringToDate(startTime, pattern);
			Date endDate = DateUtil.stringToDate(endTime, pattern);
			dataResult = iMonthDataRepository.audioMoreReport(startDate, endDate, projectCodes, mnList);
		}
		return audioMoreReportsAll_Echarts(projectResultEntity, projectTotaList, stationNameMap, projectCodes,
				dataResult, startTime, endTime, mnList, pattern);
	}

	@SuppressWarnings({ "unchecked" })
	public ResultEntity audioMoreReportsAll_Echarts(ResultEntity projectResultEntity,
			List<Map<String, Object>> projectTotaList, Map<String, Object> stationNameMap, List<String> projectCodes,
			List<Map<String, Object>> dataResult, String startTime, String endTime, List<String> mnList,
			String pattern) {
		// 查询因子值数据集
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();// 结果集
		if (dataResult == null || dataResult.size() == 0) {
			Map<String, Object> messageMap = new HashMap<String, Object>();
			messageMap.put("message", "");
			messageMap.put("project", projectTotaList);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list)
					.setInfo(messageMap);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();// 存储每一条数据
		Map<String, Object> valueMap = new HashMap<String, Object>();// 水质等级计算，存储记录类
		List<WaterInstructionsDataDto> InstructionsList = new ArrayList<WaterInstructionsDataDto>();
		List<String> keyPollution = new ArrayList<String>();// 主要污染物
		WaterInstructionsDataDto keyPollutionInstruc = new WaterInstructionsDataDto();// 计算当前污染物
		List<WaterInstructionsDataDto> keyPollutionList = new ArrayList<WaterInstructionsDataDto>();
		// 获得因子信息
		Map<String, Project> projectMap = null;
		if (projectResultEntity.getCode() == 0) {
			projectMap = (Map<String, Project>) ((Map<String, Project>) projectResultEntity.getContent()).get("info");
		}
		// 遍历因子值数据集，设置相关的值
		for (Map<String, Object> dataResultMap : dataResult) {
			String mn = dataResultMap.get("MN").toString();
			String projectCode = dataResultMap.get("CODE").toString();
			String value = dataResultMap.get("VALUE").toString();
			String projectName = "";
			String decimalDigits = "3";
			if (projectMap != null) {
				Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(projectCode),
						Project.class);
				projectName = project.getProjectName();
				decimalDigits = project.getDecimalDigits();
			}
			value = ScaleUtil.sciCalProjectString(value, decimalDigits);// 数据修约
			// 如果当前站点下没有因子相关信息，则创建新的，否则在原有的上追加
			if (!valueMap.containsKey(mn)) {
				resultMap = new HashMap<String, Object>();
				keyPollution = new ArrayList<String>();
				WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();// 计算水质等级封装类
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				InstructionsList.add(Instructions);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterQuery = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put("mn", mn);// 站点mn
				resultMap.put("name", stationNameMap.get(mn));// 站点名称
				resultMap.put(projectCode, value);// 因子编号和因子值
				resultMap.put("level", waterQuery);// 实际水质等级
				resultMap.put("waterCondition", WaterLevelUtils.calculateProject(waterQuery));// 水质状况（优良轻度污染等）
				resultMap.put("compositeIndex", WaterQualityUtil.countCompositeIndex(Instructions));// 综合水质
				// 计算当前污染物是否超过三级
				if ((WaterQualityUtil.getWaterLevel(projectCode, value) > 3) && !keyPollution.contains(projectName)) {
					keyPollution.add(projectName);
				} // 计算设置主要污染物
				resultMap.put("keyPollution", StringUtils.join(keyPollution, ","));
				list.add(resultMap);
				valueMap.put(mn, Instructions);
			} else {
				WaterInstructionsDataDto Instructions = (WaterInstructionsDataDto) valueMap.get(mn);
				ReflexUtil.setValue(Instructions, WaterInstructionsDataDto.class, projectCode, String.class, value);
				WaterQualityUtil.setWaterQuality(InstructionsList);
				String waterQuery = WaterLevelUtils.exchangeWaterLevelToChinese(Instructions.getWaterQuality());
				resultMap.put(projectCode, value);
				resultMap.put("level", waterQuery);
				resultMap.put("waterCondition", WaterLevelUtils.calculateProject(waterQuery));
				resultMap.put("compositeIndex", WaterQualityUtil.countCompositeIndex(Instructions));
				// 计算当前污染物是否超过三级
				keyPollutionInstruc = new WaterInstructionsDataDto();
				keyPollutionList = new ArrayList<WaterInstructionsDataDto>();
				ReflexUtil.setValue(keyPollutionInstruc, WaterInstructionsDataDto.class, projectCode, String.class,
						value);
				keyPollutionList.add(keyPollutionInstruc);
				WaterQualityUtil.setWaterQuality(keyPollutionList);
				if ((keyPollutionInstruc.getWaterQuality() != null)
						&& (Integer.parseInt(keyPollutionInstruc.getWaterQuality()) > 3)
						&& !keyPollution.contains(projectName)) {
					keyPollution.add(projectName);
				}
				// 计算设置主要污染物
				resultMap.put("keyPollution", StringUtils.join(keyPollution, ","));
			}
		}
		// 添加因子值数据集中不存在的站点
		List<String> mnTotalStringList = list.stream().map(l -> l.get("mn").toString()).collect(Collectors.toList());// 结果集中的站点mn集合
		String mnTotalString = StringUtils.join(mnTotalStringList, "");// 转化成字符串
		for (String mn : mnList) {
			if (!mnTotalString.contains(mn)) {
				Map<String, Object> mnMap = new HashMap<String, Object>();
				mnMap.put("mn", mn);
				mnMap.put("name", stationNameMap.get(mn));
				mnMap.put("keyPollution", "");
				mnMap.put("waterCondition", "");
				mnMap.put("compositeIndex", "");
				mnMap.put("level", "");
				list.add(mnMap);
				mnTotalString = mnTotalString + mn;
			}
		}
		// 因子值数据集中不存在的因子，需要全部设置为空
		for (String codeString : projectCodes) {
			for (Map<String, Object> map : list) {
				if (!map.containsKey(codeString)) {
					map.put(codeString, "");
				}
			}
		}
		// 封装echarts图表
		// 初始化echarts
		ChartsVo chartsVo = new ChartsVo();
		List<String> xAxis = new ArrayList<String>(); // X轴数据集
		// List<String> legend = new ArrayList<String>(); // 图例数据集
		List<Map<String, Object>> series = new ArrayList<Map<String, Object>>(); // 系列列表数据集
		Map<String, Object> seriesMap = new HashMap<String, Object>();
		// 通过时间分组
		List<String> keyList = new ArrayList<String>(list.get(0).keySet());
		TreeMap<String, List<Map<String, Object>>> glist = list.stream()
				.collect(Collectors.groupingBy(g -> g.get("name").toString(), TreeMap::new, Collectors.toList()));
		glist.forEach((k, klist) -> {
			xAxis.add(k);
			keyList.forEach(key -> {
				List<String> data = klist.stream().map(m -> m.get(key).toString()).collect(Collectors.toList());
				if (seriesMap.containsKey(key)) {
					List<String> dataSingle = (List<String>) seriesMap.get(key);
					dataSingle.addAll(data);
				} else {
					seriesMap.put(key, data);
				}
			});
		});// projectTotaList
			// 获得因子信息
		for (Entry<String, Object> string : seriesMap.entrySet()) {
			Map<String, Object> map = new HashMap<String, Object>();
			String unit = "";
			String name = "";
			if (string.getKey().equals("mn") || string.getKey().equals("name")
					|| string.getKey().equals("waterCondition") || string.getKey().equals("keyPollution")
					|| string.getKey().equals("level") || string.getKey().equals("compositeIndex")) {
				continue;
			}
			if (projectMap != null) {// 拿到因子信息
				Project project = MyBeanUtils.convertMapToBean((Map<String, Object>) projectMap.get(string.getKey()),
						Project.class);
				unit = project.getProjectUnit();
				name = project.getProjectName();
			}
			map.put("code", string.getKey());
			map.put("name", name);
			map.put("unit", unit);
			map.put("data", string.getValue());
			series.add(map);
		}
		chartsVo.setxAxis(xAxis);
		chartsVo.setSeries(series);
		if (xAxis == null || xAxis.size() == 0 || series == null || series.size() == 0) {
			return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(chartsVo);
	}

}
